package com.titlecertification.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.*;

/**
 * @author MJH
 * @date 2021-10-27
 * @Description
 */
@Controller
public class UserController {

    private String randomStr="QWERTYUIOPASDFGHJKLZXCVBNM";

    @Autowired
    private HttpServletRequest request;


    public String login(){

        return null;
    }

    /**
     * 图形验证码获取  base64
     * @return
     */
    @GetMapping("/captcha")
    @ResponseBody
    public String getCaptCha(){
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100);
        captcha.setGenerator(new RandomGenerator(randomStr,4));
        captcha.setBackground(new Color(2, 2, 2, 83));
        //验证码
        String code = captcha.getCode();
        HttpSession session = request.getSession();
        session.setAttribute("captcha",code);

        String imageBase64 = captcha.getImageBase64();

        return "data:image/png;base64,"+imageBase64;
    }
}
